<template>
	<view class="sign_in">
		<view class="main">
			<view class="title">注册账号</view>
			<view class="input">
				  <!-- #ifndef APP-NVUE -->
					<u-input placeholder="请输入姓名" v-model="username" border="surround">
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					<u--input placeholder="请输入姓名" v-model="username" border="surround">
					<!-- #endif -->
						<u--text
							text="*"
							slot="prefix"
							margin="0 3px 0 0"
							type="error"
						></u--text>
					<!-- #ifndef APP-NVUE -->
					</u-input>
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					</u--input>
					<!-- #endif -->
			</view>
			<view class="input">
				<!-- #ifndef APP-NVUE -->
				<u-input placeholder="请输入手机号" v-model="mobile">
				<!-- #endif -->
				<!-- #ifdef APP-NVUE -->
				<u--input placeholder="请输入手机号" v-model="mobile">
				<!-- #endif -->
					<u--text
						text="*"
						slot="prefix"
						margin="0 3px 0 0"
						type="error"
					></u--text>
					<template slot="suffix">
						<u-code
							ref="uCode"
							@change="codeChange"
							:seconds="sendTime"
							changeText="X秒重新获取"
						></u-code>
						<u-button
							@tap="getCode"
							:text="tips"
							type="primary"
							size="mini"
						></u-button>
					</template>
				<!-- #ifndef APP-NVUE -->
				</u-input>
				<!-- #endif -->
				<!-- #ifdef APP-NVUE -->
				</u--input>
				<!-- #endif -->
			</view>
			<view class="input">
				<view class="pass">
					<u--input
					    placeholder="请输入验证码"
					    border="surround"
					    v-model="code"
					  ></u--input>
				</view>
				<!-- <text class="btn" @click="sendCodeBtn">{{codeTxt}}</text> -->
			</view>
			<view class="input">
				<u-input v-model="pass" maxlength='16' type="password" placeholder='请输入8-16位数字、英文组合密码' />
			</view>
			<view class="input">
				<u-input v-model="pass_two" maxlength='16' type="password" placeholder='请再次输入8-16位数字、英文组合密码' />
			</view>
			<view class="input">
				<u-input v-model="invitation_code" type="text" placeholder='请输入邀请码' />
			</view>
			<view class="change_txt">
				<image v-if="changeIndex" @click="changeBtn" src="@/static/images/change_icon2.png" mode=""></image>
				<!-- <image v-else @click="changeBtn" src="@/static/images/change_icon1.png" mode=""></image> -->
				<!-- <text class="tip">我已阅读并同意</text>
				<text class="txt" @click="textBtn(1)">《用户协议》</text>
				<text class="tip">和</text>
				<text class="txt" @click="textBtn(2)">《隐私政策》</text> -->
			</view>
			<view class="in_btn" @click="registerBtn">立即注册</view>
			<!-- <view class="bot_txt" @click="siginBtn">登录</view> -->
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mobile:'',
				code:'',
				pass:'',
				pass_two:'',
				invitation_code:'',
				wx:'',
				// 
				changeIndex:false,
				username:'',
				//
				disabled:false,
				codeTxt:'发送验证码',
				sendTime:60,
				time:null,
				tips: '',
				value: ''
			};
		},
		onLoad(opt) {
			if(opt.share_code){
				this.invitation_code=opt.share_code
			}
		},
		methods:{
			codeChange(text) {
				this.tips = text;
			  },
			  getCode() {
				if (this.$refs.uCode.canGetCode) {
				  // 模拟向后端请求验证码
				  this.sendCodeBtn();
				} else {
				  uni.$u.toast('倒计时结束后再发送');
				}
			  },
			  change(e) {
				console.log('change', e);
			  },
			// 
			textBtn(val){
				uni.navigateTo({
					url:'/pages/other/text?type='+val
				})
			},
			// 发送验证码
			sendCodeBtn(){
				var that = this
				if(that.disabled){
					return
				}
				if(that.mobile=='' || !that.$test.phoneTrue(that.mobile)){
					that.$tips.showToast('请输入正确的手机号')
					return
				}else{
					that.$tips.showLoading('发送中')
					var params = {
						mobile:that.mobile,
						event:'register'
					}
					that.$httpApi.smsSend(params).then((res)=>{
						that.$tips.hideLoading()
						if(res.code==1){
							if (!that.timer) {
							  that.disabled = true
							  that.codeTxt = that.sendTime+'s'
							  that.timer = setInterval(() => {
							    if (that.sendTime > 0 && that.sendTime <= 60) {
							      that.sendTime--
							      that.codeTxt = that.sendTime+'s'
							    } else {
							      that.disabled = false
							      that.sendTime = 60
							      that.codeTxt = '重新获取'
							      clearInterval(that.timer)
							      that.timer = null;
							    }
							  }, 1000)
							}
						}
					})
				}
			},
			//协议
			// changeBtn(type){
			// 	this.changeIndex=!this.changeIndex
			// },
			// 注册
			registerBtn(){
				var that = this
				if(that.username == ''){
					that.$tips.showToast('请输入用户名')
					return
				}else if(that.mobile=='' || !that.$test.phoneTrue(that.mobile)){
					that.$tips.showToast('请输入正确的手机号')
					return
				}else if(that.pass==''){
					that.$tips.showToast('请输入密码')
					return
				}else if(that.pass.length<6){
					that.$tips.showToast('密码格式不正确')
					return
				}else if(that.pass_two==''){
					that.$tips.showToast('请再次输入密码')
					return
				}else if(that.pass!=that.pass_two){
					that.$tips.showToast('密码不一致')
					return
				}else{
					that.$tips.showLoading('加载中')
					var params = {
						username:that.username,
						mobile:that.mobile,						
						password:that.pass,
						towpassword:that.pass_two,
						invitation_code:that.invitation_code,
						wx:that.wx,
					}
					that.$httpApi.userRegister(params).then((res)=>{
						that.$tips.hideLoading()
						if(res.code==1){
							that.$store.commit('SET_TOKEN',res.data.userinfo.token)
							that.$store.dispatch('GetUserInfo')
							uni.switchTab({
								url:'/pages/shop/shop'
							})
						}
					})
				}
			},
			// 登录
			siginBtn(){
				uni.navigateBack()
			},
		}
	}
</script>

<style lang="less">
	.sign_in{
		width: 100%;
		height: 100vh;
		.background{
			position: fixed;
			top: 0;
			left: 0;
			z-index: 1;
			display: block;
			width: 100%;
			height: 100%;
		}
		.main{
			position: relative;
			box-sizing: border-box;
			z-index: 2;
			width: 100%;
			padding: 150rpx 65rpx 50rpx;
			.title{
				width: 100%;
				font-size: 50rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: 200;
				color: #666;
			}
			.input{
				margin-top: 40rpx;
				background:rgba(190, 190, 190, 0.1);
			}
			.change_txt{
				width: 100%;
				margin-top: 30rpx;
				display: flex;
				align-items: center;
				image{
					display: block;
					width: 32rpx;
					height: 32rpx;
					margin-right: 12rpx;
				}
				.tip{
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #888888;
				}
				.txt{
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: var(--color-primary);
				}
			}
			.in_btn{
				margin-top: 100rpx;
				width: 100%;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				color: #fff;
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				background: var(--color-primary);
				border-radius: 20rpx;
			}
			.bot_txt{
				width: 100%;
				margin-top: 100rpx;
				text-align: center;
				font-size: 32rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #26C281;
			}
		}
	}
</style>
